/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller;

import View.CustomBoard;
import View.ImagePanel;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import javax.swing.JButton;
import javax.swing.JEditorPane;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

/**
 *
 * @author Harsh
 */
public class ImagePanelController {
    
    private ImagePanel panelView;
    private static CustomBoard customView;
    private static CustomBoardController customController;
    private String name;
    private URL helpURL;
    private static boolean DEBUG = false;
    private JEditorPane htmlPane;
    
    public ImagePanelController(ImagePanel pane) {
        
        panelView = pane;
        pane.addImagePanelList(new ImageControlListener());
        
    }
    
    public class ImageControlListener implements ActionListener {

        public void actionPerformed(ActionEvent e) {
        if(e.getActionCommand().equals("NEW GAME"))
        {
            JOptionPane.showMessageDialog(null,"New Game");
            name = JOptionPane.showInputDialog("Please enter your name");
            
            customView = new CustomBoard();
            customController = new CustomBoardController(customView, name);
            panelView.killPanel();
            
            
            
        }//End making a new game

        else if(e.getActionCommand().equals("LOAD GAME"))
        {
            JOptionPane.showMessageDialog(null, "");
            
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!
//            !!!LAURAH!!!    
            
            
            
            
            
            
            
            
            
        }//End loading game
        

        else if(e.getActionCommand().equals("QUIT"))
        {
            int answer = JOptionPane.showConfirmDialog(null,"Are you sure to quit game?", "Warning", JOptionPane.YES_OPTION, JOptionPane.NO_OPTION);
            if(answer == JOptionPane.YES_OPTION)
            {
                System.exit(0);
            }

        }//End exiting game
        
       else if(e.getActionCommand().equals("HELP"))
            {
                
                JFrame newFrame = new JFrame("New Window");
                
                 htmlPane = new JEditorPane();
                 htmlPane.setEditable(false);
                 System.out.println("About to call help method");
                 gameHelp();
                 JScrollPane htmlView = new JScrollPane(htmlPane);

                 newFrame.setSize(new Dimension(800, 600));
                 newFrame.setLocationRelativeTo(null);
                 newFrame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);

                 newFrame.add(htmlView);
                 newFrame.setVisible(true);

            }//End method help
           
    }//End method actionPerformed

    private void gameHelp(){
              String s = "SEWebsite3.html";
              helpURL = getClass().getResource(s);
             System.out.println("This is the URL " + helpURL);
                 if (helpURL == null) {
                    System.err.println("Couldn't open help file: " + s);
                 } else if (DEBUG) {
                    System.out.println("Help URL is " + helpURL);
                 }

              displayURL(helpURL);

    }
     private void displayURL(URL url) {
            System.out.println("I'm inside display url method");

         try {
            if (url != null) {
                System.out.println("my url is not null");
                 htmlPane.setPage(url);
                 System.out.println("setting url pane");

            } else { //null url
              htmlPane.setText("File Not Found");
                if (DEBUG) {
                    System.out.println("Attempted to display a null URL.");
                }
            }
        } catch (IOException e) {
            System.err.println("Attempted to read a bad URL: " + url);
        }
    }//End display URL
        }
        
    }
